
import 'package:flutter/material.dart';
import 'package:word_range_client/pojo/vo/WordBookVo.dart';
import 'package:word_range_client/widgets/general/PartingLine.dart';

import '../../viewModels/WordBookViewModel.dart';
import '../../widgets/BookInfo/BookInfoCard.dart';
import '../../widgets/bookInfo/WordList.dart';

class BookInfoPage extends StatelessWidget {
  const BookInfoPage({super.key, required this.id});

  final String id;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("词书详情"),
      ),
      body: _BookInfoBody(id: id,),
    );
  }
}


class _BookInfoBody extends StatefulWidget {
  const _BookInfoBody({super.key, required this.id});
  final String id;

  @override
  State<_BookInfoBody> createState() => _BookInfoBodyState();
}

class _BookInfoBodyState extends State<_BookInfoBody> {

  bool _isLoading = true;
  WordBookVo? wordBookVo;

  @override
  void initState() {
    super.initState();
    getWordBookInfo();
  }

  void getWordBookInfo() {
    WordBookViewModel wordBookViewModel = WordBookViewModel();
    wordBookViewModel.getBookInfo(widget.id).then((value) {
      setState(() {
        wordBookVo =  WordBookVo.fromJson(value.data);
        _isLoading = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_isLoading) {
      return Hero(
          tag: "Book_${widget.id}",
          child: const Scaffold(
            body: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  // 转圈
                  CircularProgressIndicator(),
                  SizedBox(height: 10,),
                  Text("Loading ...."),
                ],
              ),
            ),
          )
      );
     }
    return Hero(
        tag: "Book_${widget.id}",
        child: Container(
          padding: const EdgeInsets.all(15),
          child: Column(
            children: [
              BookInfoCard(bookVo: wordBookVo!),
              const PartingLine(),
              Expanded(
                  child: Wordlist(
                    bookId: widget.id,
                    wordCount: wordBookVo!.wordCount!,
                  )
              )
            ],
          ),
        )
    );
  }
}




